#include "Print.h"

using namespace std;

Print::Print(int _ln)
    : Stmt(_ln)
{
}


Print::~Print(void)
{
    for (auto iter = elements.begin(); iter != elements.end(); ++iter)
    {
        delete *iter;
    }
}

int Print::Eval()
{
    auto elementIter = elements.begin();
    auto splitIter = splits.begin();
    char lastSplit = 0;

    while (elementIter != elements.end())
    {
        (*elementIter)->DoPrint();
        if (splitIter != splits.end())
        {
            char currSplit = *splitIter;
            switch (currSplit)
            {
            case ';':
                putchar(' ');
                break;
            case ',':
                putchar('\t');
                break;
            case 0:
                break;
            default:
                assert(0);
                break;
            }
            splitIter++;
            if (splitIter == splits.end())
            {
                lastSplit = currSplit;
            }
        }        
        elementIter++;
    }
    if (lastSplit == 0)
        printf("\n");

    return -1;
}

void Print::AddString(std::string str)
{
    elements.push_back(new StringElement(str));
}
void Print::AddExpr(Expr *expr)
{
    elements.push_back(new ExprElement(expr));
}
void Print::AddSplit(char split)
{
    splits.push_back(split);
}
